package com.ge.healthcare.autosc.ui.prodiagsscheduler.service.impl;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.ge.healthcare.autosc.common.database.dao.CfgProDiagsScheduleDAO;
import com.ge.healthcare.autosc.common.database.dao.HstProdiagsScheduleDAO;
import com.ge.healthcare.autosc.common.database.dao.IRequestDAO;
import com.ge.healthcare.autosc.common.database.pojo.ActRequest;
import com.ge.healthcare.autosc.common.database.pojo.CfgProDiagsSchedule;
import com.ge.healthcare.autosc.common.database.pojo.HstProdiagsSchedule;
import com.ge.healthcare.autosc.ui.prodiagsscheduler.dto.ScheduleBean;
import com.ge.healthcare.autosc.ui.prodiagsscheduler.service.intf.ProDiagsConnectivityService;


/*
 *
 * Copyright (C) 2011 General Electric Company. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of the
 * General Electric Company (GE). You shall not disclose this software and
 * shall use it only in accordance with the terms of the license agreement
 * you entered into with GE.
 *
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 * PURPOSE, OR NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES
 * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING
 * THIS SOFTWARE OR ITS DERIVATIVES. 
 */

public class ProDiagsConnectivityServiceImpl implements ProDiagsConnectivityService {

	private final Logger logger = Logger.getLogger(ProDiagsConnectivityServiceImpl.class);
	private IRequestDAO proDiagsConnectivityDAO;
	private HstProdiagsScheduleDAO hstProdiagsScheduleDAO;
	private CfgProDiagsScheduleDAO cfgProDiagsScheduleDAO;
	
	public void setHstProdiagsScheduleDAO(HstProdiagsScheduleDAO hstProdiagsScheduleDAO) {
		this.hstProdiagsScheduleDAO = hstProdiagsScheduleDAO;
	}
	
	public void setCfgProDiagsScheduleDAO(
			CfgProDiagsScheduleDAO cfgProDiagsScheduleDAO) {
		this.cfgProDiagsScheduleDAO = cfgProDiagsScheduleDAO;
	}

	public void setProDiagsConnectivityDAO(IRequestDAO proDiagsConnectivityDAO) {
		this.proDiagsConnectivityDAO = proDiagsConnectivityDAO;
	}

	public ScheduleBean viewSchedule(Long systemId) {
		// TODO Auto-generated method stub
		return null;
	}
	
	public List<ScheduleBean> loadSProdiagSchedule(ScheduleBean searchBean) {
		// TODO Remove dummy list and get from db
		List<ScheduleBean> scheduleBeans = new ArrayList<ScheduleBean>();
		List<ActRequest> actRequestBeans = new ArrayList<ActRequest>();
		if (searchBean.getSystemId()!= null ) {
			actRequestBeans = proDiagsConnectivityDAO.getRequestBySystemID(searchBean.getSystemId());
		}
		if (searchBean.getModelType()!= null || searchBean.getDropDownModelType()!=null) {
			actRequestBeans = proDiagsConnectivityDAO.getRequestByModelType(StringUtils.isNotBlank(searchBean.getModelType())?searchBean.getModelType():searchBean.getDropDownModelType());

		}
			Calendar cal = Calendar.getInstance();
			for (ActRequest actRequest : actRequestBeans) {
				System.out.println("currDate "+actRequest.getStartTime());
				cal.setTime(actRequest.getStartTime());
				scheduleBeans.add(new ScheduleBean(actRequest.getSystemId(),actRequest.getModelType(), actRequest.getSystemType(), "Placeholder description for Task 1", 1, cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.HOUR), cal.get(Calendar.MINUTE), cal.get(Calendar.WEEK_OF_MONTH),searchBean.getType()));
		}
		return scheduleBeans;
	}

	public List<ScheduleBean> compareProDiagsConfig(ScheduleBean searchBean) {
		List<ScheduleBean> scheduleBeans = new ArrayList<ScheduleBean>();
		List<HstProdiagsSchedule> hstProdigasScheduleList = hstProdiagsScheduleDAO.getAllObjects();
		for (HstProdiagsSchedule hstProdigasSchedule : hstProdigasScheduleList) {
			scheduleBeans.add(new ScheduleBean(hstProdigasSchedule.getSystemId(),hstProdigasSchedule.getModelType(),hstProdigasSchedule.getTaskName(),
					hstProdigasSchedule.getIterations(),hstProdigasSchedule.getDay(),hstProdigasSchedule.getHour(),hstProdigasSchedule.getMinute(),hstProdigasSchedule.getWeek(),
					new Date()));
		}
		
		List<CfgProDiagsSchedule> cfgProDiagsScheduleList = cfgProDiagsScheduleDAO.getAllObjects();
		for (CfgProDiagsSchedule cfgProDiagsSchedule : cfgProDiagsScheduleList) {
			scheduleBeans.add(new ScheduleBean(cfgProDiagsSchedule.getSystemId(), cfgProDiagsSchedule.getModelType(), cfgProDiagsSchedule.getTaskName(),
					cfgProDiagsSchedule.getIterations(), cfgProDiagsSchedule.getDay(), cfgProDiagsSchedule.getHour(), cfgProDiagsSchedule.getMinute(), cfgProDiagsSchedule.getWeek(),
					new Date()));
		}
			
		return scheduleBeans;
	}

	public List<ScheduleBean> report(ScheduleBean searchBean) {
		List<ScheduleBean> scheduleBeans = new ArrayList<ScheduleBean>();
		List<HstProdiagsSchedule> hstProdigasScheduleList = hstProdiagsScheduleDAO.getAllObjects();
		int i=1;
		for (HstProdiagsSchedule hstProdigasSchedule : hstProdigasScheduleList) {
			if(StringUtils.isNotBlank(searchBean.getRequestId())){
				scheduleBeans.add(new ScheduleBean(searchBean.getRequestId(), "501902876",hstProdigasSchedule.getUpdatedDate(),"Success","Comments"));
			} else {
				scheduleBeans.add(new ScheduleBean(String.valueOf(i++), "501902876",hstProdigasSchedule.getUpdatedDate(),"Success","Comments"));
			}
		}
		
		List<CfgProDiagsSchedule> cfgProDiagsScheduleList = cfgProDiagsScheduleDAO.getAllObjects();
		for (CfgProDiagsSchedule cfgProDiagsSchedule : cfgProDiagsScheduleList) {
			if(StringUtils.isNotBlank(searchBean.getRequestId())){
				scheduleBeans.add(new ScheduleBean(searchBean.getRequestId(), "501902876",cfgProDiagsSchedule.getUpdatedDate(),"Success","Comments"));
			} else {
				scheduleBeans.add(new ScheduleBean(String.valueOf(i++), "501902876",cfgProDiagsSchedule.getUpdatedDate(),"Success","Comments"));
			}
		}
			return scheduleBeans;
	}
	public List<Integer> getDayList() {
		List<Integer> dayList = new ArrayList<Integer>();
		for(int i =1 ; i<31; i++){
			dayList.add(i);
		}
		return dayList;
	}
	public List<Integer> getHourList() {
		List<Integer> hourList = new ArrayList<Integer>();
		for(int i =1 ; i<25; i++){
			hourList.add(i);
		}
		return hourList;
	}
	public List<Integer> getMinuteList() {
		List<Integer> minuteList = new ArrayList<Integer>();
		for(int i =1 ; i<25; i++){
			minuteList.add(i);
		}
		return minuteList;
	}
	public List<Integer> getWeekList() {
		List<Integer> weekList = new ArrayList<Integer>();
		for(int i =1 ; i<8; i++){
			weekList.add(i);
		}
		return weekList;
	}

}
